<extend name="Public/base" />

<block name="body">
    <!-- 标题栏 -->
    <div class="main-title">
        <h2>{$meta_title}</h2>
    </div>

    <div class="cf">

        <div class="data-table table-striped">
            <table>
                <thead>
                <tr>
                    <th>学科</th>
                    <th>章节 - 知识点</th>
                    <th>题目</th>
                    <th>解析</th>
                    <th>类型</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <notempty name="check_list">
                    <volist name="check_list" id="vo">
                        <tr>

                            <td>{$vo.sid|getSubjectName}</td>
                            <td>{$vo.one_chapter_id|getChapterName} - {$vo.two_chapter_id|getChapterName}</td>
                            <td>{$vo.question}</td>
                            <td>{$vo.analysis}</td>
                            <td><if condition="$vo['style'] == 1">单选<else />多选</if></td>
                            <td>
                                <a href="{:U('delVideoTreeExercises',array('id'=>$vo['id']))}" class="authorize">删除</a>
                            </td>
                        </tr>
                    </volist>
                    <else/>
                    <td colspan="8" class="text-center"> aOh! 暂时还没有内容! </td>
                </notempty>
                </tbody>
            </table>
        </div>
        <div class="page">
            {$check_page}
        </div>

        <br />
        <br />
        <br />

        <div class="fl">
            <a class="btn" onclick="confirmAdd()"> 确 认 新 增 习 题 </a>
        </div>
        <!-- 高级搜索 -->
        <div class="search-form fr cf">
            <div class="sleft">
                <select name="sid" class="subject">
                    <option value="0">请选择学科</option>
                    <volist name="subject_list" id="vo">
                        <option value="{$vo.id}" <if condition="$vo['id'] eq I('sid')">selected</if>>{$vo.name}</option>
                    </volist>
                </select>
                <select name="one_chapter_id" class="one_chapter">
                    <option value="0">请选择章节</option>
                </select>
                <select name="two_chapter_id" class="two_chapter">
                    <option value="0">请选择知识点</option>
                </select>

                <select name="style">
                    <option value="0">请选择类型</option>
                    <option value="1" <if condition="I('style') == 1">selected</if>>单选</option>
                    <option value="2" <if condition="I('style') == 2">selected</if>>多选</option>
                </select>

                <input type="hidden" value="{:I('id')}" name="id">
            </div>
            <a class="sch-btn" href="javascript:;" id="search" url="{:U('videoTreeExercisesList')}"><i class="btn-search"></i></a>
        </div>
    </div>
    <!-- 数据列表 -->
    <div class="data-table table-striped">
        <table>
            <thead>
            <tr>
                <th><input type="checkbox" class="all_check" /></th>
                <th>学科</th>
                <th>章节 - 知识点</th>
                <th>题目</th>
                <th>解析</th>
                <th>类型</th>
            </tr>
            </thead>
            <tbody>
            <notempty name="_list">
                <volist name="_list" id="vo">
                    <tr>

                        <td><input type="checkbox" name="ids" class="single_check" value="{$vo.id}" /></td>
                        <td>{$vo.sid|getSubjectName}</td>
                        <td>{$vo.one_chapter_id|getChapterName} - {$vo.two_chapter_id|getChapterName}</td>
                        <td>{$vo.question}</td>
                        <td>{$vo.analysis}</td>
                        <td><if condition="$vo['style'] == 1">单选<else />多选</if></td>
                    </tr>
                </volist>
                <else/>
                <td colspan="7" class="text-center"> aOh! 暂时还没有内容! </td>
            </notempty>
            </tbody>
        </table>
    </div>
    <div class="page">
        {$_page}
    </div>
</block>

<block name="script">
    <script src="__STATIC__/thinkbox/jquery.thinkbox.js"></script>
    <script type="text/javascript">
        $(function (){
            var subject_list = {$subject_list_json};
            var sid = "{:I('sid')}";
            var one_chapter_id = "{:I('one_chapter_id')}";
            var two_chapter_id = "{:I('two_chapter_id')}";

            if(subject_list.length > 0){
                var option = "<option value='0'>请选择章节</option>";
                var two_option = "<option value='0'>请选择知识点</option>";
                //渲染学科下拉列表
                for(var i = 0; i < subject_list.length; i++){
                    if(subject_list[i].id == sid){
                        chapter_list = subject_list[i].chapter_list;
                        //渲染章节下拉列表
                        for (var j = 0; j < subject_list[i].chapter_list.length; j++) {
                            var obj = subject_list[i].chapter_list[j];
                            var one_selected = '';
                            if(one_chapter_id == obj.id) one_selected = 'selected';
                            option += "<option value='"+ obj.id +"' "+ one_selected +">"+ obj.name +"</option>";

                            if(one_chapter_id == obj.id){
                                var two_chapter_list = obj.son_list;
                                if(two_chapter_list.length > 0){
                                    //渲染知识点下拉列表
                                    for (var k = 0; k < two_chapter_list.length; k++) {
                                        var two_obj = two_chapter_list[k];
                                        var two_selected = '';
                                        if(two_chapter_id == two_obj.id){
                                            two_selected = 'selected';
                                        }
                                        two_option += "<option value='"+ two_obj.id +"' "+ two_selected +">"+ two_obj.name +"</option>";
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
                $('.one_chapter').html(option);
                $('.two_chapter').html(two_option)
            }
        })

        $('.subject').change(function (){
            var subject_list = {$subject_list_json};
            var sid = $(this).val();

            if(subject_list.length > 0){
                var option = "<option value='0'>请选择章节</option>";
                for(var i = 0; i < subject_list.length; i++){
                    if(subject_list[i].id == sid){
                        chapter_list = subject_list[i].chapter_list;
                        for (var j = 0; j < subject_list[i].chapter_list.length; j++) {
                            var obj = subject_list[i].chapter_list[j];
                            option += "<option value='"+ obj.id +"'>"+ obj.name +"</option>";
                        }
                        break;
                    }
                }
                $('.one_chapter').html(option);
                $('.two_chapter').html('<option value="0">请选择知识点</option>')
            }
        })

        var chapter_list = [];
        $('.one_chapter').change(function (){
            var one_chapter_id = $(this).val();
            if(chapter_list.length){
                var option = "<option value='0'>请选择知识点</option>";
                for(var i = 0; i < chapter_list.length; i++){
                    if(chapter_list[i].id == one_chapter_id){
                        for (var j = 0; j < chapter_list[i].son_list.length; j++) {
                            var obj = chapter_list[i].son_list[j];
                            option += "<option value='"+ obj.id +"'>"+ obj.name +"</option>";
                        }
                        break;
                    }
                }
                $('.two_chapter').html(option);
            }
        })

        //搜索功能
        $('#search').click(function(){
            var url = $(this).attr('url').replace('.html', '');
            var query  = $('.search-form').find('input').serialize();
            var squery = $('.search-form').find('select').serialize();
            query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
            query = query.replace(/^&/g,'');
            squery=squery.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
            squery=squery.replace(/^&/g,'');
            if( url.indexOf('?')>0 ){
                url += '&' + query+'&'+squery;
            }else{
                url += '?' + query+'&'+squery;
            }
            window.location.href = url;
        });


        //导航高亮
        highlight_subnav('{:U("Manage/videoList")}');
    </script>

    <script type="text/javascript">
        $('.all_check').click(function (){
            var is_check = $(this).is(':checked');
            if(is_check){
                $("input[class='single_check']").prop("checked",true);
            }else{
                $("input[class='single_check']").prop("checked",false);
            }
        })

        $('.single_check').click(function (){
            var single_check_len = $('.single_check').length;
            var check_len = $("input[class='single_check']:checked").length;

            if(single_check_len <= check_len){
                $("input[class='all_check']").prop("checked",true);
            }else{
                $("input[class='all_check']").prop("checked",false);
            }
        })

        function confirmAdd()
        {
            var id = "{:I('id')}";
            var check = $("input[class='single_check']:checked");
            if(check.length > 0){
                var ids = '';
                check.each(function (i, v){
                    var id = $(v).val();
                    if(ids == ''){
                        ids = id;
                    }else{
                        ids += ',' + id;
                    }
                })

                var url = "{:U('addVideoTreeExercises')}";
                $.post(url, {
                    ids: ids,
                    id: id
                }, function (res){
                    layer.msg(res.msg)
                    if(res.result == 1){
                        setTimeout(function () {
                            window.location.href = window.location.href;
                        }, 500)
                    }
                },'json')

            }else{
                layer.msg('请勾选需要新增的题目');
            }
        }
    </script>
</block>